<?php

namespace app\admin\controller\litestore;

use app\common\controller\Backend;
use app\common\logic\mall\Order;
use think\exception\DbException;
use think\response\Json;

/**
 * 商城订单退款管理
 *
 * @icon fa fa-circle-o
 */
class OrderRefund extends Backend
{

    /**
     * OrderRefund模型对象
     * @var \app\admin\model\litestore\OrderRefund
     */
    protected $model = null;

    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\litestore\OrderRefund;
        $this->view->assign("refundTypeList", $this->model->getRefundTypeList());
        $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
        $this->view->assign("isDelList", $this->model->getIsDelList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->with(['user', 'goods'])
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        //退款金额
        $refund_amount = $this->model->where($where)->where('refund_status','=',$this->model::REFUND_STATUS_COMPLETE)->sum('refund_money');

        $result = ['total' => $list->total(), 'rows' => $list->items(), 'extend' => ['refund_amount' => (float)$refund_amount]];

        return json($result);
    }

    public function detail($ids = null)
    {
        $row = $this->model->get($ids, ['user', 'goods', 'order_info']);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $row['refund_images'] = $row['refund_images'] ? explode(',', $row['refund_images']) : [];

        $this->view->assign('row', $row);
        return $this->view->fetch();
    }

    public function pass($ids = null)
    {
        $row = $this->model->get($ids, ['order_info']);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }

        $params = $this->request->post('row/a');

        if ($row->refund_status > $this->model::REFUND_STATUS_APPLY) {
            $this->error(__('Operated, please do not repeat the operation'));
        }

        $orderLogic = new Order();

        $result = $orderLogic->refundVerify($row, $this->auth->getUserInfo(), 0, $params);

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids, ['order_info']);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($row->refund_status > $this->model::REFUND_STATUS_APPLY) {
            $this->error(__('Operated, please do not repeat the operation'));
        }

        $params = $this->request->post();

        $orderLogic = new Order();

        $result = $orderLogic->refundVerify($row, $this->auth->getUserInfo(), 1, [], $params['remark']);

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }

    public function complete($ids = null)
    {
        $row = $this->model->get($ids, ['order_info']);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($row->refund_status > $this->model::REFUND_STATUS_COMPLETE) {
            $this->error(__('Operated, please do not repeat the operation'));
        }

        $orderLogic = new Order();

        $result = $orderLogic->refundComplete($row, $this->auth->getUserInfo());

        if (!$result) {
            $this->error($orderLogic->getError());
        }

        $this->success(__('Operation completed'));
    }
}
